package com.bawei.mvvm.http;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitManager {
    private RetrofitManager(){}
    private static RetrofitManager retrofitManager;
    public static RetrofitManager getInstance(){
        if(retrofitManager == null){
            synchronized (RetrofitManager.class){
                if(retrofitManager == null){
                    retrofitManager = new RetrofitManager();
                }
            }
        }
        return retrofitManager;
    }

    private Retrofit mRetrofit;

    public Retrofit getRetrofit() {
        if(mRetrofit == null){
            createRetrofit();
        }
        return mRetrofit;
    }

    private void createRetrofit() {
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();


        mRetrofit = new Retrofit.Builder()
                .baseUrl(ConstantUtils.BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//RxJava2CallAdapterFactory将okhttp返回的Call数据转成Observable
                .addCallAdapterFactory(new LiveDataCallAdapterFactory())//自定义工厂：okhttp返回Call---》LiveData
                .build();
    }
}
